package com.jtlrm.ckd.mvp.view.adapter;

import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.base.sdk.util.CustomeInputFilter;
import com.base.sdk.util.DataUtil;
import com.base.sdk.util.EditInputFilter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jtlrm.ckd.R;
import com.jtlrm.ckd.entity.ShengHuaZhiBiaoEntity;

public class ShengHuaDetailAdapter extends BaseQuickAdapter<ShengHuaZhiBiaoEntity, BaseViewHolder> {

    boolean isAdd;

    public ShengHuaDetailAdapter(boolean isAdd) {
        super(R.layout.shenghua_check_detail_item);
        this.isAdd = isAdd;
    }


    @Override
    protected void convert(final BaseViewHolder helper, final ShengHuaZhiBiaoEntity item) {
        helper.setText(R.id.label, item.getName());
        helper.setText(R.id.danwei, "(" + DataUtil.subZeroAndDot(item.getMinValue()) + "~" + DataUtil.subZeroAndDot(item.getMaxValue()) + ") " + item.getUnit());
        EditText contentE = helper.getView(R.id.content);
        ImageView iconI = helper.getView(R.id.icon);
        if (isAdd) {
            contentE.setEnabled(true);
            iconI.setVisibility(View.GONE);
            // 避免错乱
            if (contentE.getTag() instanceof TextWatcher) {
                contentE.removeTextChangedListener((TextWatcher) contentE.getTag());
            }
            CustomeInputFilter filter;
            if ("31".equals(item.getSort()) || "33".equals(item.getSort())) {
                // C超敏反应蛋白,24小时尿量 0-12000
                filter = new CustomeInputFilter(mContext, 12000);
            } else if ("13".equals(item.getSort())) {
                // 空腹血糖
                filter = new CustomeInputFilter(mContext, 60);
            } else if ("6".equals(item.getSort())) {
                //血尿素氮
                filter = new CustomeInputFilter(mContext, 100);
            } else if ("5".equals(item.getSort())) {
                //血清肌酐
                filter = new CustomeInputFilter(mContext, 10000);
            } else {
                filter = new CustomeInputFilter(mContext);
            }
            InputFilter[] filters = {filter};
            contentE.setFilters(filters);
            helper.setText(R.id.content, item.getValue());
            TextWatcher textWatcher = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    String str = s.toString();
                    item.setValue(str);
                }
            };
            contentE.addTextChangedListener(textWatcher);
            contentE.setTag(textWatcher);
        } else {
            helper.setText(R.id.content, item.getValue());
            contentE.setEnabled(false);
            try {
                if (TextUtils.isEmpty(item.getValue()) || TextUtils.isEmpty(item.getMaxValue()) || TextUtils.isEmpty(item.getMinValue())) {
                    iconI.setVisibility(View.GONE);
                } else {
                    Double value = Double.parseDouble(item.getValue());
                    Double max = Double.parseDouble(item.getMaxValue());
                    Double min = Double.parseDouble(item.getMinValue());
                    if (value < min) {
                        iconI.setVisibility(View.VISIBLE);
                        iconI.setImageResource(R.drawable.down_float);
                    } else if (value > max) {
                        iconI.setVisibility(View.VISIBLE);
                        iconI.setImageResource(R.drawable.up_float);
                    } else {
                        iconI.setVisibility(View.GONE);
                    }
                }
            } catch (Exception e) {
                iconI.setVisibility(View.GONE);
            }
        }
    }

}

